package cn.wp.ewallet.upms.dao.impl;

import cn.wp.common.core.dao.JdbcSupportTemplate;
import cn.wp.common.core.dao.SqlString;
import cn.wp.common.core.model.WpPage;
import cn.wp.ewallet.upms.dao.DemoDao;
import cn.wp.ewallet.upms.model.entity.DemoDo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 样例
 * Created by chenyb on 2019/09/28
 */
@Repository
public class DemoDaoImpl implements DemoDao {

    @Autowired
    private JdbcSupportTemplate jdbcSupportTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public int insertDemo(DemoDo demoDo) {
        return jdbcSupportTemplate.insert(demoDo);
    }

    @Override
    public int updateDemo(DemoDo demoDo) {
        return jdbcSupportTemplate.update(demoDo);
    }

    @Override
    public int deleteDemoById(String id) {
        return jdbcSupportTemplate.deleteById(id,DemoDo.class);
    }

    @Override
    public int deleteDemo(DemoDo demoDo) {
        return jdbcSupportTemplate.delete(demoDo);
    }

    @Override
    public List<DemoDo> selectDemoList(DemoDo demoDo) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select * from t_demo where name  = ?");
        Object[] params = new Object[]{
                demoDo.getName()
        };
        return jdbcSupportTemplate.selectList(sqlString.build(),params,DemoDo.class);
    }

    @Override
    public WpPage<DemoDo> selectDemoList(DemoDo demoDo, WpPage wpPage) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select * from t_demo");

        Object[] params = new Object[]{
                demoDo.getName()
        };
        return jdbcSupportTemplate.selectList(sqlString.build(),null,wpPage,DemoDo.class);
    }

    @Override
    public int countDemo(DemoDo demoDo) {
        return jdbcSupportTemplate.count("select count(*) from t_demo",null);
    }

    @Override
    public DemoDo selectDemoById(String id) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select * from t_demo where id = ?");
        return jdbcSupportTemplate.selectObject(sqlString.build(),new Object[]{id},DemoDo.class);
    }

    @Override
    public int updateType(DemoDo demoDo) {
        return jdbcSupportTemplate.execute("update name data_type = 1 where id = ?",new Object[]{demoDo.getId()});
    }

    @Override
    public DemoDo selectDemoByCache(DemoDo demoDo) {
        return (DemoDo)redisTemplate.opsForValue().get(demoDo.getId());
    }

}
